
import time
import urllib.request
import urllib.parse
import json
import hashlib
import base64
#接口地址
url ="http://ltpapi.xfyun.cn/v1/ke"
#开放平台应用ID
x_appid = "25d6e7c3"
#开放平台应用接口秘钥
api_key = "81fa9d0996faca80de3963e574ed041c"



def main(TEXT):
    body = urllib.parse.urlencode({'text': TEXT}).encode('utf-8')
    param = {"type": "dependent"}
    x_param = base64.b64encode(json.dumps(param).replace(' ', '').encode('utf-8'))
    x_time = str(int(time.time()))
    x_checksum = hashlib.md5(api_key.encode('utf-8') + str(x_time).encode('utf-8') + x_param).hexdigest()
    x_header = {'X-Appid': x_appid,
                'X-CurTime': x_time,
                'X-Param': x_param,
                'X-CheckSum': x_checksum}
    req = urllib.request.Request(url, body, x_header)
    result = urllib.request.urlopen(req)
    result = result.read()
    print(result.decode('utf-8'))
    # 把'bytes'类型的数据转换成'string'之后再用弄成字典，最后再打印提取出list第一个数据也就是权重最大的数据
    dicts = json.loads(result.decode('utf-8'))
    result =dicts['data']['ke'][0]['word']
    print(dicts['data']['ke'][0]['word'])


    return result


if __name__ == '__main__':
    main(TEXT)
